package com.sise.wangzhan.controller;

import com.sise.wangzhan.cache.CacheService;
import com.sise.wangzhan.collapse.CollapseService;
import com.sise.wangzhan.entity.Person;
import com.sise.wangzhan.service.MyException;
import com.sise.wangzhan.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * @Classname InvokerController
 * @Description TODO
 * @Date 2020/5/6 20:39
 * @Created by wangzhan
 */
@RestController
@Configuration
public class InvokerController {

    @Autowired
    private PersonService personService;
    @Autowired
    private CacheService cacheService;
    @Autowired
    private CollapseService collapseService;

    @RequestMapping(value = "/router/{personid}",method = RequestMethod.GET,
    produces = MediaType.APPLICATION_JSON_VALUE)
    public Person router(@PathVariable Integer personid){
        Person person = personService.getPerson(personid);
        return person;
    }

    /************************测试配置*******************/
    @RequestMapping(value = "/testConfig",method = RequestMethod.GET)
    public String testConfig() throws InterruptedException {
        String result = personService.testConfig();

        return result;
    }

    @RequestMapping(value = "/testException",method = RequestMethod.GET)
    public String testException() throws MyException {
        String result = personService.testException();

        return result;
    }


    /************************测试缓存注解*******************/
    @RequestMapping(value = "/cache1/{personid}",method = RequestMethod.GET,
                    produces = MediaType.APPLICATION_JSON_VALUE)
    public Person testCacheResult(@PathVariable Integer personid){
        // 调用多次服务
        for (int i = 0; i < 3; i++) {
            Person person = cacheService.getPerson(personid);
            System.out.println("控制器调用服务：" + i);
        }
        return new Person();
    }


    @RequestMapping(value = "/cache2",method = RequestMethod.GET,
                    produces = MediaType.APPLICATION_JSON_VALUE)
    public String testCacheRemove(){
        for (int i = 0; i < 3; i++) {
            cacheService.cacheMethod("a");
            System.out.println("控制器调用服务："+i);
        }

        cacheService.updateMethod("a");
        System.out.println("================= 缓存失效================");

        for (int i = 0; i < 3; i++) {
            cacheService.cacheMethod("a");
            System.out.println("控制器调用服务："+ i);
        }
        return "";
    }

    /************************测试合并请求注解*******************/
    @RequestMapping(value = "/collapse", method = RequestMethod.GET,
                    produces = MediaType.APPLICATION_JSON_VALUE)
    public String testCollapse() throws ExecutionException, InterruptedException {
        //连续执行3次请求
        Future<Person> f1 = collapseService.getSinglePerson(1);
        Future<Person> f2 = collapseService.getSinglePerson(2);
        Future<Person> f3 = collapseService.getSinglePerson(3);

        Person p1 = f1.get();
        Person p2 = f2.get();
        Person p3 = f3.get();

        System.out.println(p1.getId()+"----"+p1.getName());
        System.out.println(p2.getId()+"----"+p2.getName());
        System.out.println(p3.getId()+"----"+p3.getName());

        return "";
    }

}
